package com.ruoyi.tws.controller;

import java.util.List;
import java.io.IOException;
import java.util.Arrays;
import javax.servlet.http.HttpServletResponse;

import lombok.RequiredArgsConstructor;
import com.ruoyi.common.security.annotation.PreAuthorize;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.core.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.Query;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ruoyi.common.core.web.page.Condition;
import com.ruoyi.tws.domain.TwsWarn;
import com.ruoyi.tws.service.ITwsWarnService;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 预警Controller
 * 
 * @author ruoyi
 * @date 2023-06-29
 */
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/warn" )
public class TwsWarnController extends BaseController
{
    private final ITwsWarnService iTwsWarnService;

    /**
     * 查询预警列表
     */
    @PreAuthorize(hasPermi="tws:warn:list")
    @GetMapping("/list")
    public TableDataInfo list(Query query, TwsWarn twsWarn)
    {
        LambdaQueryWrapper<TwsWarn> lqw = new LambdaQueryWrapper<TwsWarn>();
        if (StringUtils.isNotBlank(twsWarn.getContent())) {
            lqw.eq(TwsWarn::getContent, twsWarn.getContent());
        }
        if (StringUtils.isNotBlank(twsWarn.getIsHandle())) {
            lqw.eq(TwsWarn::getIsHandle, twsWarn.getIsHandle());
        }
        lqw.orderByDesc(TwsWarn::getCreateTime);
        IPage<TwsWarn> list = iTwsWarnService.page(Condition.getPage(query), lqw);
        return getDataTable(list);
    }

    /**
     * 导出预警列表
     */
    @PreAuthorize(hasPermi = "tws:warn:export")
    @Log(title = "预警", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TwsWarn twsWarn) throws IOException
    {
        LambdaQueryWrapper<TwsWarn> lqw = new LambdaQueryWrapper<TwsWarn>(twsWarn);
        List<TwsWarn> list = iTwsWarnService.list(lqw);
        ExcelUtil<TwsWarn> util = new ExcelUtil<TwsWarn>(TwsWarn. class);
        util.exportExcel(response, list, "warn");
    }

    /**
     * 获取预警详细信息
     */
    @PreAuthorize(hasPermi="tws:warn:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(iTwsWarnService.getById(id));
    }

    /**
     * 新增预警
     */
    @PreAuthorize(hasPermi="tws:warn:add")
    @Log(title = "预警", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TwsWarn twsWarn) {
        return toAjax(iTwsWarnService.save(twsWarn) ? 1 : 0);
    }

    /**
     * 修改预警
     */
    @PreAuthorize(hasPermi="tws:warn:edit")
    @Log(title = "预警" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TwsWarn twsWarn) {
        return toAjax(iTwsWarnService.updateById(twsWarn) ? 1 : 0);
    }

    /**
     * 删除预警
     */
    @PreAuthorize(hasPermi="tws:warn:remove")
    @Log(title = "预警", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(iTwsWarnService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }

}
